package edu.scnu.fans.fingertip.item.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import java.nio.charset.StandardCharsets;

/**
 * 测试流响应
 */
@RestController
public class StreamingController {

    @GetMapping("/fingertip/item/v1/stream")
    public ResponseEntity<StreamingResponseBody> chat() {
        StreamingResponseBody responseBody = response -> {

            for (int i = 0; i < 200; i++) {
                response.write((i + ",").getBytes(StandardCharsets.UTF_8));
                response.flush();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        return ResponseEntity.ok()
                .header("Content-Type", "text/event-stream;charset=UTF-8")
                .body(responseBody);
    }

}
